var SinaWeiBo = {
    REQUEST_TOKEN_URL: 'http://api.t.sina.com.cn/oauth/request_token',
    AUTHORIZE_URL: 'http://api.t.sina.com.cn/oauth/authorize',
    ACCESS_TOKEN_URL: 'http://api.t.sina.com.cn/oauth/access_token',

    UPDATE_API_URL: "http://api.t.sina.com.cn/statuses/update.json",

    username: "",
    password: ""
}

SinaWeiBo.oauth_init = function() {
    var self = this;
    ﻿
    var message = new Message(SinaWeiBo.REQUEST_TOKEN_URL);
    ﻿
    ﻿Browser.XML_HTTP_REQUEST.onload = function(aEvent) {
        ﻿﻿﻿
        var text = aEvent.target.responseText;
        Utils.log("Request Token Sucess : " + text);
        ﻿﻿﻿
        var p = Utils.decompose_urlparamstr(text);
        ﻿﻿﻿
        if (p['oauth_token']) {
            ﻿﻿﻿﻿
            return self.oauth_authorize(p['oauth_token'], p['oauth_token_secret']);
            ﻿﻿﻿
        } else {
            ﻿﻿﻿﻿Utils.log("request Token Error");
            ﻿﻿﻿﻿
            return false;
            ﻿﻿﻿
        }
        ﻿﻿
    };
    ﻿Browser.openHttpRequest(message);
    ﻿Browser.sendHttpRequest(message.sign());﻿
};

SinaWeiBo.oauth_authorize = function(oauthToken, oauthTokenSecret) {
    var self = this;
    Utils.log("Start oauth authorization." + [this.username, this.password]);
    ﻿
    var paramMap = {};
    ﻿paramMap['oauth_token'] = oauthToken;
    ﻿paramMap['oauth_callback'] = 'xml';
    ﻿paramMap['userId'] = self.username;
    ﻿paramMap['passwd'] = self.password;
    ﻿
    ﻿
    var message = {
        action: this.AUTHORIZE_URL,
        method: 'POST'
    };

    ﻿Browser.XML_HTTP_REQUEST.onload = function(aEvent) {
        ﻿﻿
        var text = aEvent.target.responseText;
        var parser = new DOMParser();
        ﻿﻿﻿
        var oauthVerifier = parser.parseFromString(text, "text/xml").childNodes[0].childNodes[1].firstChild.textContent
        Utils.log("Authorize Sucess : " + oauthVerifier);
        return self.oauth_access_token(oauthToken, oauthTokenSecret, oauthVerifier);
        ﻿﻿
    };
    ﻿Browser.openHttpRequest(message);
    ﻿Browser.sendHttpRequest(paramMap);
};

SinaWeiBo.oauth_access_token = function(oauthToken, oauthTokenSecret, oauthVerifier) {
    ﻿
    var message = new Message(SinaWeiBo.ACCESS_TOKEN_URL);
    ﻿message.setOAuthToken(oauthToken);
    ﻿message.setOAuthTokenSecret(oauthTokenSecret);
    ﻿message.setOAuthVerifier(oauthVerifier);

    ﻿Browser.XML_HTTP_REQUEST.onload = function(aEvent) {
        ﻿﻿﻿
        var text = aEvent.target.responseText;
        ﻿﻿﻿Utils.log("Acess Token Sucess : " + text);
        ﻿﻿﻿
        var result = Utils.decompose_urlparamstr(text);
        ﻿﻿﻿
        if (result['oauth_token']) {
            ﻿﻿﻿﻿Utils.log("OAuth Sucess!");
            ﻿﻿﻿﻿Browser.Preferences.setCharPref('oauth.access_token', result['oauth_token']);
            ﻿﻿﻿﻿Browser.Preferences.setCharPref('oauth.access_token_secret', result['oauth_token_secret']);
            ﻿﻿﻿﻿
            return true;
            ﻿﻿﻿
        } else {
            ﻿﻿﻿Utils.log("Get access_token error!");
            ﻿﻿﻿﻿
            return false;
            ﻿﻿﻿
        }
        ﻿﻿
    };

    ﻿Browser.openHttpRequest(message);
    ﻿Browser.sendHttpRequest(message.sign());
}

SinaWeiBo.postMessage = function(word) {
	Utils.log("Start to post word.")	﻿
    var message = new Message(SinaWeiBo.UPDATE_API_URL);
    ﻿message.setOAuthToken(Browser.Preferences.getCharPref('oauth.access_token'));
    ﻿message.setOAuthTokenSecret(Browser.Preferences.getCharPref('oauth.access_token_secret'));
    ﻿message.setMessage(word);
    ﻿
    ﻿Browser.XML_HTTP_REQUEST.onload = function(aEvent) {
        Utils.log(aEvent.target.responseText)
        return true;
    };

    ﻿Browser.openHttpRequest(message);
    ﻿Browser.sendHttpRequest(message.sign());
};

SinaWeiBo.isLogin = function() {
    var hasAccessToken = Browser.Preferences.prefHasUserValue('oauth.access_token');
    var hasAccessTokenSecret = Browser.Preferences.prefHasUserValue('oauth.access_token_secret');

    return hasAccessToken && hasAccessTokenSecret
}

SinaWeiBo.login = function(name, password) {
    this.username = name;
    this.password = password;

    ﻿Browser.Preferences.QueryInterface(Components.interfaces.nsIPrefBranch2);
    ﻿
    return SinaWeiBo.oauth_init();
}